package vy.org.app.page.member;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.vy.dal.dao.ViewVyLookupPatrikaPeriodDAO;
import org.vy.dal.dao.ViewVyLookupPatrikaPeriodDO;
import org.vy.dal.dao.lookup.ViewVyLookupCitiesDAO;
import org.vy.dal.dao.lookup.ViewVyLookupCitiesDO;
import org.vy.dal.dao.lookup.VyLookupDeliveryModeDAO;
import org.vy.dal.dao.lookup.VyLookupDeliveryModeDO;
import org.vy.dal.dao.lookup.VyLookupPatrikaDAO;
import org.vy.dal.dao.lookup.VyLookupPatrikaDO;

import vy.org.app.common.BaseAction;
import vy.org.app.common.IdTypeEnum;
import vy.org.app.common.ModuleEnum;
import vy.org.app.page.address.AddressForm;
import vy.org.app.page.lookup.delivery.DeliveryModeHelper;
import vy.org.app.page.lookup.delivery.MntnDeliveryModeForm;
import vy.org.app.page.lookup.patrika.MntnPatrikaPeriodForm;
import vy.org.app.page.lookup.patrika.PatrikaHelper;
import vy.org.app.page.subscription.dabba.DabbaForm;
import vy.org.app.page.subscription.magazine.MagazineForm;

public class AddMemberAction extends BaseAction {

	@Override
	public ActionForward executeAction(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		
		MemberForm form = (MemberForm)actionForm;
		
		try{
			List<VyLookupPatrikaDO> patrikaLst = VyLookupPatrikaDAO.findAllActive();
			List<ViewVyLookupPatrikaPeriodDO> patPeriodLst = ViewVyLookupPatrikaPeriodDAO.findAllActive();
			List<VyLookupDeliveryModeDO> deliveryModeLst = VyLookupDeliveryModeDAO.findAllActive();
			
			Map<String, List<MntnPatrikaPeriodForm>> patrikaMap = new HashMap<String, List<MntnPatrikaPeriodForm>>(); 
			Map<String, MntnDeliveryModeForm> delModeMap = new HashMap<String, MntnDeliveryModeForm>(); 
			
			for(VyLookupPatrikaDO pat : patrikaLst){
				patrikaMap.put(pat.getPatrikaId().toString(), PatrikaHelper.fillPatrikaPeriodForm(pat, patPeriodLst));
			}
			for(VyLookupDeliveryModeDO dl : deliveryModeLst){
				delModeMap.put(dl.getDeliveryModeCode(), DeliveryModeHelper.fillPatrikaForm(dl));
			}
			form.setPatrikas(patrikaMap);
			form.setDeliveryModes(delModeMap);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		try{
			List<ViewVyLookupCitiesDO> countryLst = ViewVyLookupCitiesDAO.findUniqueCountries();
			List<ViewVyLookupCitiesDO> stateLst1=null,stateLst2=null,stateLst3=null,cityLst1=null,cityLst2=null,cityLst3=null;

			form.getPresentAddress().setCountries(countryLst);
			form.getPermanentAddress().setCountries(countryLst);
			form.getOfficeAddress().setCountries(countryLst);
			
			AddressForm prtAddr = form.getPresentAddress();
			if(prtAddr != null){
				if(prtAddr.getCountry() != null && !"".equals(prtAddr.getCountry()))
					stateLst1 = ViewVyLookupCitiesDAO.findStateByCountry(prtAddr.getCountry());
				if(prtAddr.getStateOrRegion() != null && !"".equals(prtAddr.getStateOrRegion()))
					cityLst1 = ViewVyLookupCitiesDAO.findCitiesByStateAndCountry(prtAddr.getStateOrRegion(), prtAddr.getCountry());
			}
			AddressForm pmtAddr = form.getPermanentAddress();
			if(pmtAddr != null){
				if(pmtAddr.getCountry() != null && !"".equals(pmtAddr.getCountry()))
					stateLst2 = ViewVyLookupCitiesDAO.findStateByCountry(pmtAddr.getCountry());
				if(pmtAddr.getStateOrRegion() != null && !"".equals(pmtAddr.getStateOrRegion()))
					cityLst2 = ViewVyLookupCitiesDAO.findCitiesByStateAndCountry(pmtAddr.getStateOrRegion(), pmtAddr.getCountry());
			}
			AddressForm offAddr = form.getOfficeAddress();
			if(offAddr != null){
				if(offAddr.getCountry() != null && !"".equals(offAddr.getCountry()))
					stateLst3 = ViewVyLookupCitiesDAO.findStateByCountry(offAddr.getCountry());
				if(offAddr.getStateOrRegion() != null && !"".equals(offAddr.getStateOrRegion()))
					cityLst3 = ViewVyLookupCitiesDAO.findCitiesByStateAndCountry(offAddr.getStateOrRegion(), offAddr.getCountry());
			}
			form.getPresentAddress().setStates(stateLst1);
			form.getPermanentAddress().setStates(stateLst2);
			form.getOfficeAddress().setStates(stateLst3);
			form.getPresentAddress().setCities(cityLst1);
			form.getPermanentAddress().setCities(cityLst2);
			form.getOfficeAddress().setCities(cityLst3);
			
		}catch(Exception e) {
			e.printStackTrace();
			return mapping.findForward("failure");
		}		

		if(ModuleEnum.ADD_SUBSCRIPTION.getName().equals(form.getMode())){
			List<MagazineForm> mzForms = null;
			if(form.getMagazines() != null)
				mzForms = form.getMagazines();
			else
				mzForms = new ArrayList<MagazineForm>();

			MagazineForm mzForm = new MagazineForm();
			mzForm.setPatrikaName("ADD");
			mzForm.setVyId(form.getVyId());
			mzForms.set(form.getMagazineSubscriptionCnt(), mzForm);
			form.setMagazineSubscriptionCnt(form.getMagazineSubscriptionCnt()+1);
			return mapping.findForward("failure");
		}
		
		if(ModuleEnum.ADD_PATRA.getName().equals(form.getMode())){
			List<DabbaForm> daForms = null;
			daForms = form.getDabbas();
			DabbaForm daForm = new DabbaForm();
			daForm.setId(form.getVyId());
			daForm.setIdType(IdTypeEnum.MEMBER.getId().toString());
			daForms.add(daForm);
			daForms.set(form.getDabbaSubscriptionCnt(), daForm);
			form.setDabbaSubscriptionCnt(form.getDabbaSubscriptionCnt()+1);
			return mapping.findForward("failure");
		}

		return mapping.findForward("success");
	}

	@Override
	protected ModuleEnum getModuleId() {
		return ModuleEnum.ADD_MEMBER;
	}
	
}
